import DebugPanel from "../UI/DebugPanel";
import MapManager from "./MapManager";
import GameManager from "./GameManager";
import { GameData } from "../Data/GameData";
import Grid from "../Game/Grid";
import Block from "../Game/Block";
import CardFunctions from "../Common/CardFunctions";
import { LevelData_1001 } from "../Data/Level/LevelData_1001";
import CommonFunction from "../utils/CommonFunction";

const { ccclass, property } = cc._decorator;

@ccclass
export default class InputManager extends cc.Component {
    static Instance: InputManager;

    onLoad() {
        InputManager.Instance = this;
    }

    start() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }

    上() {
        // MapManager.Instance.Change(+1);
    }

    下() {
        // MapManager.Instance.MoveDown();
    }

    左() {
    }

    右() {
    }

    async onKeyDown(event: KeyboardEvent, customDate) {
        if (event.keyCode == cc.macro.KEY.up || event.keyCode == cc.macro.KEY.w) {
            // console.log(`up`);
        }
        if (event.keyCode == cc.macro.KEY.down || event.keyCode == cc.macro.KEY.s) {
            // console.log(`down`);
        }
        if (event.keyCode == cc.macro.KEY.left || event.keyCode == cc.macro.KEY.a) {
            // console.log(`left`);
        }
        if (event.keyCode == cc.macro.KEY.right || event.keyCode == cc.macro.KEY.d) {
            // console.log(`right`);
        }

        if (event.keyCode == cc.macro.KEY.space) {
            console.log(`空格`);

            // let data = LevelData_1001;
            // CommonFunction.saveJSON(data, "12345");

            // let a = CardFunctions.生成id俄罗斯方块(10);
            // console.log(a);
            //  a = CardFunctions.生成id俄罗斯方块(3);
            // console.log(a);
            //  a = CardFunctions.生成id俄罗斯方块(2);
            // console.log(a);
            //  a = CardFunctions.生成id俄罗斯方块(1);
            // console.log(a);
            
            // let a = CardFunctions.GetAllItemArray();
            let b = CardFunctions.GetAllBlockGroup();
            // console.log(b);

            return;

            // 示例数组
            const array = [
                { id: 1, num: 5 },
                { id: 2, num: 2 },
                { id: 3, num: 3 },
                { id: 4, num: 1 },
                { id: 5, num: 5 },
                { id: 1, num: 4 },
            ];

            // 使用 find 方法查找数组中的第一个偶数
            const firstEven = array.find(element => element.id === 10);
            console.log("firstEven", firstEven);



            // for (let index = 0; index < GameData.arrBoard.length; index++) {
            //     const board = GameData.arrBoard[index];
            //     console.log(board);
            //     console.log(board.node.x, board.node.y);
            //     console.log(board.x, board.y);
            // }

        }
        if (event.keyCode == cc.macro.KEY.escape) {
            GameManager.Instance.Fail();
        }
        if (event.keyCode == cc.macro.KEY.t) {
            DebugPanel.Instance.main.active = !DebugPanel.Instance.main.active;
            // if (DebugPanel.Instance.main.active == false) {
            //     DebugPanel.Instance.ShowOn();
            // } else {
            //     DebugPanel.Instance.ShowOff();
            // }
        }
        // if (event.keyCode == cc.macro.KEY.f1) {
        //     console.log("f1");
        //     CardDebugPanel.Instance.main.active = !CardDebugPanel.Instance.main.active;
        // }

        if (event.keyCode == cc.macro.KEY.f) {
            // console.log("f");
        }

        if (event.keyCode == cc.macro.KEY.c) {
            // console.log("c");
        }

        if (event.keyCode == cc.macro.KEY.num1) {
            console.log("num1");
        }
        if (event.keyCode == cc.macro.KEY.num2) {
            console.log("num2");
        }
        if (event.keyCode == cc.macro.KEY.num3) {
            console.log("num3");
        }
        if (event.keyCode == cc.macro.KEY.num4) {
            console.log("num4");
        }
    }

    onKeyUp(event: KeyboardEvent, customDate): void {
        if (event.keyCode == cc.macro.KEY.down || event.keyCode == cc.macro.KEY.s) {
            // console.log(`down`);
            // if (GameManager.Instance.isCanOperate) {
            // MapManager.Instance.MoveDown();
            // GameData.speed = GameData.speed_normal;
            GameData.是否按住键盘下 = false;
            // }
        }
    }
}